import q0107

# playing around with vimos pointings for the Q0107 proposal.

def plot_vimos(xc,yc,col='b',rotation=90,arrows=False):
    """Given a central pointing and position angle (90 deg - horizontal or
    0 deg - vertical), plot a vimos pointing on a ra-dec (degrees vs
    degrees) plot."""

    # widths and heights in degrees 
    if rotation == 0:
        w1 = (6 + 43./60) / 60.
        w2 = (6 + 41./60) / 60.
        w3 = (6 + 48./60) / 60.
        w4 = (6 + 59./60) / 60.
        h1 =  (8 + 1./60) / 60.
        h2 =  (8 + 1./60) / 60.
        h3 =  8 / 60.
        h4 =  (8 + 5./60) / 60.
        hspace1 = 58. / 3600
        hspace2 = 59. / 3600
        hspace3 = 60. / 3600
        hspace4 = 60. / 3600
        vspace1 = 77. / 3600
        vspace2 = 64. / 3600
        vspace3 = 67. / 3600
        vspace4 = 58. / 3600
    elif rotation == 90:
        h1 = (6 + 41./60) / 60.
        h2 = (6 + 59./60) / 60.
        h3 = (6 + 43./60) / 60.
        h4 = (6 + 48./60) / 60.
        w1 =  (8 + 1./60) / 60.
        w2 =  (8 + 5./60) / 60.
        w3 =  (8 + 1./60) / 60.
        w4 =  8 / 60.
        hspace1 = 64. / 3600
        hspace2 = 58. / 3600
        hspace3 = 77. / 3600
        hspace4 = 67. / 3600
        vspace1 = 59. / 3600
        vspace2 = 60. / 3600
        vspace3 = 58. / 3600
        vspace4 = 60. / 3600

    ax = pl.gca()
    # centre point and N/E directions
    ax.plot([xc],[yc],'xr')
    ax.axis('equal')
    if arrows:
        ax.arrow(xc,yc,xc,yc+h1/2.,fc='r',ec='r')
        ax.text(xc+hspace1/4.,yc+h1/2.,'N')
        ax.arrow(xc,yc,xc-w1/2.,yc,fc='r',ec='r')
        ax.text(xc-w1/2.,yc+vspace1/4.,'E')

    # top left quadrant
    x1 = xc - hspace1 - w1
    x2 = x1 + w1
    y1 = yc + vspace1
    y2 = y1 + h1
    ax.fill([x1,x2,x2,x1,x1],[y1,y1,y2,y2,y1],fc=col,alpha=0.2)

    # top right quadrant
    x1 = xc + hspace2
    x2 = x1 + w2
    y1 = yc + vspace2
    y2 = y1 + h2
    ax.fill([x1,x2,x2,x1,x1],[y1,y1,y2,y2,y1],fc=col,alpha=0.2)

    # bottom left quadrant
    x1 = xc - hspace3 - w3
    x2 = x1 + w3
    y1 = yc - vspace3
    y2 = y1 - h3
    ax.fill([x1,x2,x2,x1,x1],[y1,y1,y2,y2,y1],fc=col,alpha=0.2)

    # bottom right quadrant
    x1 = xc + hspace4
    x2 = x1 + w4
    y1 = yc - vspace4
    y2 = y1 - h3
    ax.fill([x1,x2,x2,x1,x1],[y1,y1,y2,y2,y1],fc=col,alpha=0.2)
    pl.show()

    return

if 1:
    # three main 0107 QSOs
    q = q0107.readqsos()
    # extra QSOs
    pl.clf()
    ra = [17.6995833,17.6254166]
    dec = [-2.2566, -2.2483]
    xc,yc = q[1].ra,q[1].dec
    plot(q.ra,q.dec,'sk')
    plot(ra,dec,'sk')
    pl.axis('equal')
    #x1,y1 = -2.4/60.,2/60.
    #x2,y2 = 2.5/60.,4.5/60.
    #x3,y3 = x2-2.4/60., y2+2/60.

    xoff,yoff =2.75/60, 2.2/60

    plot_vimos(xc,yc,col='b')
    plot_vimos(xc+xoff,yc+yoff,col='r')
    plot_vimos(xc-xoff,yc-yoff,col='g')
    x0,x1,y0,y1 = pl.axis()
    pl.xlim(x1,x0)
    coords0107 = [dec2s(xc,yc),dec2s(xc+xoff,yc+yoff),
                  dec2s(xc-xoff,yc-yoff)]


    # Q1148 QSOs
    pl.figure()
    radec = [('11 48 44.26' ,'10 47 56.9'),
             ('11 48 42.05' ,'10 47 09.0'),
             ('11 48 39.42' ,'10 50 16.7'),
             ('11 48 51.39' ,'10 48 25.0'),
             ('11 48 47.89' ,'10 54 59.4')]
    ra,dec = zip(*[s2dec(*rd) for rd in radec])
    xc,yc = ra[0]+4/60.,dec[0]+2./60.
    plot(ra,dec,'sk')
    pl.axis('equal')
    plot_vimos(xc,yc,col='b')
    x1,y1 = 2.4/60.,4/60.
    x2,y2 = -2.5/60.,2/60.
    plot_vimos(xc+x1,yc+y1,col='r')
    plot_vimos(xc+x2,yc+y2,col='g')
    coords1148 = [dec2s(xc,yc),dec2s(xc+x1,yc+y1),dec2s(xc+x2,yc+y2)]
